<?php
/*
Template Name: Blog  - 2 columns
*/
get_header(); ?>

<?php if ( have_posts() ) while ( have_posts() ) : the_post(); 

    $sidebar_position = get_post_meta($post->ID, 'sidebar_position', true);

    $content = get_the_content();
	$title = get_the_title();
	
	$content_class = ' class="position_left"';
	$sidebar_class = ' class="position_right"';
	
	if ( $sidebar_position == 'left' )
	{
		$content_class = ' class="position_right"';
		$sidebar_class = ' class="position_left"';
	}
	
	if ( $sidebar_position == 'website_selected' )
	{
		$website_selected_position = get_option('sidebar_position');
		
		if ( $website_selected_position == 'left' )
		{
			$content_class = ' class="position_right"';
			$sidebar_class = ' class="position_left"';
		}
	}
	
	// Posts per page
	$posts_per_page = get_post_meta($post->ID, 'posts_per_page', true);
	if ( $posts_per_page == '' ) $posts_per_page = '-1';

endwhile; ?>

<?php $blogs = get_blogs(); 

foreach ($blogs as $blog):
	if (is_page($blog->page)): ?>
	
    	<section id="content_wrapper" class="clearfix">
			<div id="content_with_sidebar" <?php echo $content_class; ?>>
            	<h1 class="page_title">
                	<?php echo $title; ?>
                </h1>
                <div class="page_content">
					<?php the_content(); ?>
				</div><!-- .page_content -->
                                
            	<?php
				
				if ( is_front_page() )
					$paged = ( get_query_var('paged') ) ? get_query_var('paged') : ( get_query_var('page') ) ? get_query_var('page') : 1;
				
				$current_categories = $blog->category;
				$wp_query = new WP_Query('post_type=post&posts_per_page='.$posts_per_page.'&cat='.$current_categories.'&paged='.$paged);

				global $more;
				$more = 0;
				$post_count = 0;
				
				if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post();
					
					if ( has_post_thumbnail() )
					{
						$thumbnail_src = wp_get_attachment_url( get_post_thumbnail_id( $post->ID ) );
					
						$thumbnail_size = 'w=300&amp;h=160';
						$image_source = get_template_directory_uri().'/includes/timthumb.php?src='.$thumbnail_src.'&amp;'.$thumbnail_size.'&amp;zc=1&amp;q=100';
					}
					
					$article_margin = '';
					if ( $post_count % 2 == 0 )
						$article_margin = 'style="margin-right: 30px;"';
					$post_count++;
                                
                	echo '<article class="blog_2_columns"'.$article_margin.'>';
						
						echo '<div class="meta clearfix">';
							echo '<div class="date">';
								echo __( 'Posted on: ', 'reFresh' ); echo get_the_date('d M Y'); 
							echo '</div>';
							
							echo '<div class="posted_by">';
								echo __( 'Posted by: ', 'reFresh' );	the_author();
							echo '</div>';
						echo '</div>';
						
						if ( has_post_thumbnail() )
						{
							echo '<a class="blog_image" href="'; the_permalink(); echo '">';
								echo '<img src="'.$image_source.'" alt="'; echo the_title(); echo '" />';
							echo '</a>';
						}

						echo '<div class="blog_content clearfix">';
								
							echo '<div class="heading">';
								echo '<a href="'; the_permalink(); echo '">';
									echo '<h3>';
										echo the_title();
									echo '</h3>';
								echo '</a>';
							echo '</div>';
							
							echo '<div class="text">';
								echo the_content('');
							echo '</div>';
							
							echo '<div class="read_more">';
								echo '<a href="'; the_permalink(); echo '">';
									echo '<span class="read_more_arrow"></span>';
									echo __( 'Read more', 'reFresh' );
								echo '</a>';
							echo '</div>';
							
						echo '</div>'; // .blog_content
						
					echo '</article>';
				
				
				
                endwhile; endif;
				
				if(function_exists('wp_pagenavi')) { wp_pagenavi(); }
				
                ?>
                
    		</div> <!-- #content_with_sidebar -->
            
            <?php
			echo '<div id="sidebar"'.$sidebar_class.'>';
				echo '<div id="sidebar_content">';
					get_sidebar();
				echo '</div>';
			echo '</div>';
			?>
            
        </section> <!-- #content_wrapper -->
    
	<?php get_footer(); 

	return;
	endif;
endforeach;
?>









